getByKey.js ➔ getByKey   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 9.75
c 0
b 0
f 0
cc 3
crap 3
1
export default function getByKey(original, key, defaultValue) {
2 3
    const keys = key.split('.');
3
4 3
    let reference = original;
5
6 3
    while (keys.length > 0) {
7 4
        const referenceKey = keys.shift();
8
9 4
        if (
10
            reference === null ||
11
            reference === undefined ||
12
            !Object.prototype.hasOwnProperty.call(reference, referenceKey)
13
        ) {
14 2
            return defaultValue;
15
        }
16 2
        reference = reference[referenceKey];
17
    }
18
19 1
    return reference;
20
}
21